package com.chixing.service.impl;

import com.chixing.mapper.CouponMapper;
import com.chixing.mapper.MyCouponMapper;
import com.chixing.pojo.Coupon;
import com.chixing.pojo.MyCoupon;
import com.chixing.pojo.MyCouponCouponVO;
import com.chixing.pojo.MyCouponExample;
import com.chixing.service.MyCouponService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 马博
 * * @date 2021/10/6
 */
@Service
public class MyCouponServiceImpl implements MyCouponService {
    @Autowired
    private MyCouponMapper myCouponMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ServerResponse getCustomerCoupon(Integer custId) {
        List<MyCouponCouponVO> myCouponCouponVOS=new ArrayList<>();
        MyCouponExample myCouponExample = new MyCouponExample();
        myCouponExample.createCriteria().andCustIdEqualTo(custId);
        List<MyCoupon> myCouponList = myCouponMapper.selectByExample(myCouponExample);
        for (MyCoupon myCoupon:myCouponList){
            Coupon coupon = couponMapper.selectByPrimaryKey(myCoupon.getCouponId());
            myCouponCouponVOS.add(new MyCouponCouponVO(myCoupon,coupon));
        }
        if (myCouponCouponVOS.size()!=0)
            return ServerResponse.getSuccess(myCouponCouponVOS);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getCustomerCoupon1(Integer custId) {
        List<MyCouponCouponVO> myCouponCouponVOS=new ArrayList<>();
        MyCouponExample myCouponExample = new MyCouponExample();
        myCouponExample.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(0);
        List<MyCoupon> myCouponList = myCouponMapper.selectByExample(myCouponExample);
        for (MyCoupon myCoupon:myCouponList){
            Coupon coupon = couponMapper.selectByPrimaryKey(myCoupon.getCouponId());
            myCouponCouponVOS.add(new MyCouponCouponVO(myCoupon,coupon));
        }
        if (myCouponCouponVOS.size()!=0)
            return ServerResponse.getSuccess(myCouponCouponVOS);
        return ServerResponse.getFail(null);
    }


    @Override
    public ServerResponse save(Integer custId, Integer couponId) {
        MyCouponExample myCouponExample = new MyCouponExample();
        MyCouponExample.Criteria criteria = myCouponExample.createCriteria();
        criteria.andCustIdEqualTo(custId);
        criteria.andCouponIdEqualTo(couponId);
        List<MyCoupon> myCoupons = myCouponMapper.selectByExample(myCouponExample);
        if(myCoupons.size() == 0){
            MyCoupon myCoupon = new MyCoupon();
            myCoupon.setCouponId(couponId);
            myCoupon.setCustId(custId);

            //优惠券开始时间
            Date date = new Date();
            long liveTime = (long) 60*60*1000*24*30;
            //优惠券失效时间
            Date date1 = new Date(date.getTime() + liveTime);

            myCoupon.setMyCouponTime(date);
            myCoupon.setMyConuponIneffectiveTime(date1);
            myCoupon.setStatus(0);
            int insert = myCouponMapper.insert(myCoupon);
            if(insert > 0){
                MyCouponExample myCouponExample1 = new MyCouponExample();
                MyCouponExample.Criteria criteria1 = myCouponExample1.createCriteria();
                criteria1.andCouponIdEqualTo(couponId);
                criteria1.andCustIdEqualTo(custId);
                MyCoupon myCoupon1 = myCouponMapper.selectByExample(myCouponExample1).get(0);
                Integer myCouponId = myCoupon1.getMyCouponId();
                redisTemplate.opsForValue().set("myCoupon_" + myCouponId,myCoupon1,liveTime, TimeUnit.MILLISECONDS);
                return ServerResponse.saveSuccess("领取成功！");
            }
            return ServerResponse.saveFail("领取失败!");
        }else
            return ServerResponse.saveFail("您已拥有该优惠券，请勿重复领取！");
    }

    @Override
    public MyCoupon getByConditions(Integer custId, Integer couponId) {
        MyCouponExample myCouponExample = new MyCouponExample();
        MyCouponExample.Criteria criteria = myCouponExample.createCriteria();
        criteria.andCustIdEqualTo(custId);
        if(couponId != null)
            criteria.andCouponIdEqualTo(couponId);
        MyCoupon myCoupon = myCouponMapper.selectByExample(myCouponExample).get(0);
        return myCoupon;
    }

    @Override
    public ServerResponse updateStatus1(MyCoupon myCoupon) {
        myCoupon.setStatus(1);
        int update = myCouponMapper.updateByPrimaryKey(myCoupon);
        if(update > 0)
            return ServerResponse.updateSuccess(myCoupon);
        return ServerResponse.updateFail(null);
    }


}
